/*
 * app_uartcom.c
 *
 *  Created on: Nov 8, 2013
 *      Author: Omar Sevilla
 */
#include "stdtypedef.h"
#include "drv_uart.h"
#include "app_uartcom.h"
#include "app_indicator.h"
#include "drv_adc8.h"

extern T_UBYTE rub_MenuCounter;
extern T_UWORD	ruw_RedValue,
				ruw_GreenValue,
				ruw_BlueValue,
				ruw_ExtRedValue,
				ruw_ExtGreenValue,
				ruw_ExtBlueValue;
/*Command Managment
 * This function prepare and send the next command
 * Is a state machine
 * 
 * In OFF Mode
 * - send OFF Command
 * 
 * In ANALOG Mode
 * - send red_select command
 * - send red_value
 * - send green_select command
 * - send green value
 * - send blue_select command
 * - send blue_value
 * 
 * In SECUENCIAL Mode
 * - send SECUENCIAL command 
 */
void cmd_mng(void){
	static T_UBYTE lub_CmdCounter = 0;
	extern T_UBYTE rub_ExtMenuCounter;
	
	if(rub_ExtMenuCounter == OFF){
		uart1_putchar(OFF_CMD);
	}
	else if(rub_ExtMenuCounter == ANALOG_MODE){
		switch(lub_CmdCounter){
		case 0:
			uart1_putchar(ANALOG_CMD);
			lub_CmdCounter++;
			break;
		case 1:
			uart1_putchar(RED_SELECT_CMD);
			lub_CmdCounter++;
			break;
		case 2:
			uart1_putchar(PWM_CMD((T_UBYTE)((float)(ruw_RedValue) / 2.55)));
			//uart1_putchar(PWM_CMD(50));
			lub_CmdCounter++;
			break;
		case 3:
			uart1_putchar(GREEN_SELECT_CMD);
			lub_CmdCounter++;
			break;
		case 4:
			uart1_putchar(PWM_CMD((T_UBYTE)((float)(ruw_GreenValue) / 2.55)));
			//uart1_putchar(PWM_CMD(0));
			lub_CmdCounter++;
			break;
		case 5:
			uart1_putchar(BLUE_SELECT_CMD);
			lub_CmdCounter++;
			break;
		case 6:
			uart1_putchar(PWM_CMD((T_UBYTE)((float)(ruw_BlueValue) / 2.55)));
			//uart1_putchar(PWM_CMD(0));
			lub_CmdCounter = 0;
			break;
		default:
			lub_CmdCounter = 0;
			break;
		}
	}
	else{
		uart1_putchar(SECUENCIAL_CMD);
	}
}

union chksumreg
{
	T_UBYTE ChksumVar;
	struct chksumstruct
	{
		T_UBYTE bit0 : 1;
		T_UBYTE bit1 : 1;
		T_UBYTE bit2 : 1;
		T_UBYTE bit3 : 1;
		T_UBYTE bit4 : 1;
		T_UBYTE bit5 : 1;
		T_UBYTE bit6 : 1;
		T_UBYTE bit7 : 1;
	}ls_ChksumStruct;
}lu_Chksumreg;

void app_chksum(){
	uart1_putchar(0x00);
	lu_Chksumreg.ChksumVar = 0x00;
}

void cmd_exc(T_UBYTE lub_Cmd){
	extern T_UWORD	ruw_ExtRedValue,
					ruw_ExtGreenValue,
					ruw_ExtBlueValue;
	static T_UBYTE lub_ColourSelected = 0;
	
	switch(lub_Cmd){
	case OFF_CMD:
		rub_MenuCounter = OFF;
		break;
	case ANALOG_CMD:
		rub_MenuCounter = ANALOG_MODE;
		break;
	case SECUENCIAL_CMD:
		rub_MenuCounter = SECUENCIAL_MODE;
		break;
	case RED_SELECT_CMD:
		lub_ColourSelected = RED_SELECTED;
		break;
	case GREEN_SELECT_CMD:
		lub_ColourSelected = GREEN_SELECTED;
		break;
	case BLUE_SELECT_CMD:
		lub_ColourSelected = BLUE_SELECTED;
		break;
	default:
		if((lub_Cmd & (0x80)) == 0x80){
				switch(lub_ColourSelected){
				case RED_SELECTED:
					ruw_ExtRedValue = (T_UWORD)(((float)(lub_Cmd & ~(0x80)))*2.55);
					break;
				case GREEN_SELECTED:
					ruw_ExtGreenValue = (T_UWORD)(((float)(lub_Cmd & ~(0x80)))*2.55);
					break;
				case BLUE_SELECTED:
					ruw_ExtBlueValue = (T_UWORD)(((float)(lub_Cmd & ~(0x80)))*2.55);
					break;
				default:
					break;
				}
		}
		break;
	}
}
